package network

import (
	"fmt"
	"gitee.com/amoyx/cloud-provider/pkg/cloud/huawei/models"
	natv2 "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/nat/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/nat/v2/model"
	regionv2 "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/nat/v2/region"
)

type NATGreeter interface {
	NAT(region string) NATInterface
}

type NATInterface interface {
	CreateNatInstance(args models.NatRequest)
	CreateNatGatewaySnatRule(natId string, cidr string, eipId string)
}

type nat struct {
	network *Network
	region  string
	client  *natv2.NatClient
}

func newNAT(network *Network, region string) NATInterface {
	v := &nat{
		network: network,
		region:  region,
	}
	_client := natv2.NewNatClient(
		natv2.NatClientBuilder().
			WithRegion(regionv2.ValueOf(region)).
			WithCredential(network.credentials).
			Build())
	v.client = _client
	return v
}

// CreateNatInstance 创建nat实例
func (n *nat) CreateNatInstance(args models.NatRequest) {
	request := &model.CreateNatGatewayRequest{}
	body := &model.CreateNatGatewayOption{
		Name:              args.Name,
		RouterId:          args.VpcId,
		InternalNetworkId: args.SubnetId,
		Description:       &args.Description,
		Spec:              model.GetCreateNatGatewayOptionSpecEnum().E_1,
	}
	request.Body = &model.CreateNatGatewayRequestBody{
		NatGateway: body,
	}
	response, err := n.client.CreateNatGateway(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

// CreateNatGatewaySnatRule 创建snat规则
func (n *nat) CreateNatGatewaySnatRule(natId string, cidr string, eipId string) {
	request := &model.CreateNatGatewaySnatRuleRequest{}
	cidrSnatRule := cidr
	snatRulebody := &model.CreateNatGatewaySnatRuleOption{
		NatGatewayId: natId,
		Cidr:         &cidrSnatRule,
		FloatingIpId: eipId,
	}
	request.Body = &model.CreateNatGatewaySnatRuleRequestOption{
		SnatRule: snatRulebody,
	}
	response, err := n.client.CreateNatGatewaySnatRule(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}
